// propertywindow.cpp : implementation file
//

#include "stdafx.h"

#include "propertywindow.h"
#include "PropertySubWindow.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define WM_UPDATEPICTURE WM_USER+1
#define WM_PARTAVIALABLE WM_USER+2

/////////////////////////////////////////////////////////////////////////////
// PropertyWindow
namespace pa
{
	IMPLEMENT_DYNCREATE(PropertyWindow, CFormView)

	PropertyWindow::PropertyWindow()
		: CFormView(PropertyWindow::IDD)
	{
	

		INIT_RESOURCE
		//{{AFX_DATA_INIT(PropertyWindow)
			// NOTE: the ClassWizard will add member initialization here
		//}}AFX_DATA_INIT
		m_pVisitor = NULL;
		
	}

	PropertyWindow::~PropertyWindow()
	{
		CWinApp* pApp = AfxGetApp();
		if(pApp == NULL)
			return;
		CWnd* pWnd = pApp->GetMainWnd();
		if(pWnd == NULL)
			return;
		pWnd->SetActiveWindow();
	}

	void PropertyWindow::DoDataExchange(CDataExchange* pDX)
	{
		CFormView::DoDataExchange(pDX);
		//{{AFX_DATA_MAP(PropertyWindow)
			// NOTE: the ClassWizard will add DDX and DDV calls here
		//}}AFX_DATA_MAP
	}


	BEGIN_MESSAGE_MAP(PropertyWindow, CFormView)
		//{{AFX_MSG_MAP(PropertyWindow)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_SHOWWINDOW()
	ON_NOTIFY_EX(TTN_NEEDTEXT, 0, ToolTipProc)
	//}}AFX_MSG_MAP
	END_MESSAGE_MAP()

	/////////////////////////////////////////////////////////////////////////////
	// PropertyWindow diagnostics

	#ifdef _DEBUG
	void PropertyWindow::AssertValid() const
	{
		CFormView::AssertValid();
	}

	void PropertyWindow::Dump(CDumpContext& dc) const
	{
		CFormView::Dump(dc);
	}
	#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// PropertyWindow message handlers




int PropertyWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	END_RESOURCE
	
	BOOL bResult = FALSE;
	for(int i = 0; i < m_pVisitor->GetSize(); i++)
	{
		bResult = m_pVisitor->GetAt(i)->Create(this,i+1);
		ASSERT(bResult);
		m_pVisitor->GetAt(i)->SetWindowPosition(GetWindowPosition(i));
		m_pVisitor->GetAt(i)->propertyWindow(this);
	}

	this->EnableToolTips();
	
	return 0;
}

void PropertyWindow::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	this->SetScrollSizes(MM_TEXT, CSize(260,10+27*m_pVisitor->GetSize()));
	
}

void PropertyWindow::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CFormView::OnShowWindow(bShow, nStatus);
	
	CFont* pFont = GetFont();
	
	for(int i = 0; i < m_pVisitor->GetSize(); i++)
	{
		m_pVisitor->GetAt(i)->SetFont(pFont);
	}
	
}


BOOL PropertyWindow::ToolTipProc( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
{
	NMTTDISPINFO *pInfo = (NMTTDISPINFO*)pTTTStruct;

//	memcpy(pInfo->szText, m_pPrompt, strlen(m_pPrompt)+1);

	UINT nID = pInfo->hdr.idFrom;
	nID = ::GetDlgCtrlID((HWND)nID);

	if(nID > m_pVisitor->GetSize())
		return TRUE;

	PropertySubWindow* pWnd = m_pVisitor->GetAt(nID - 1);
	ASSERT(pWnd);

	CString strTemp = pWnd->ShowString();

	ASSERT(strTemp.GetLength() < 80);

	memcpy(pInfo->szText, strTemp.GetBuffer(0), strTemp.GetLength() + 1);

	strTemp.ReleaseBuffer();

	return TRUE;
}

BOOL PropertyWindow::OnCommand(WPARAM wParam, LPARAM lParam) 
{
	INT nMsg = HIWORD(wParam);
	INT nID = LOWORD(wParam);
	
	GetParent()->PostMessage(WM_COMMAND, wParam, lParam);
	
	return CFormView::OnCommand(wParam, lParam);
}

void PropertyWindow::UpdatePropertyData(BOOL bState)
{
	for(int i = 0; i < m_pVisitor->GetSize(); i++)
	{
		m_pVisitor->GetAt(i)->UpdatePropertyData(bState);
	}
	if( GetParent() == NULL )
		return;

	GetParent()->SendMessage(WM_UPDATEPICTURE);
	if(m_pVisitor->GetSize() > 0)
	GetParent()->SendMessage(WM_PARTAVIALABLE);
	
}


void PropertyWindow::UpdatePropertyValidData()
{
	for(int i = 0; i < m_pVisitor->GetSize(); i++)
	{
		m_pVisitor->GetAt(i)->UpdateData();
	}
}


CRect PropertyWindow::GetWindowPosition(INT nIndex)
{
	const INT nLength = 260;
	const INT nHeight = 22;
	const INT nBlank = 5;

	return CRect(5, (nHeight + nBlank)*nIndex+5, nLength+5, (nHeight + nBlank)*nIndex + nHeight+5);
}

};
